package king.dominic.jlibrary.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;

import king.dominic.jlibrary.activity.OriginActivity;
import king.dominic.jlibrary.util.FragmentFactory;
import king.dominic.jlibrary.view.WindowUtil;

/**
 * Created by king on 2017/3/31.
 * OriginFragment
 */

public class OriginFragment extends OriginWebFragment implements FragmentFactory.FragmentParent {
    private final String TAG = getClass().getSimpleName();
    protected FragmentTitle title;
    private FragmentFactory.FragmentSaver mSaver = new FragmentFactory.FragmentSaver(this);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSaver.rescue(savedInstanceState);
    }

    public FragmentTitle getFragmentTitle() {
        return title;
    }

    public void setTitle(String title) {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).setTitleBarText(title);
        }
    }

    public <T extends View> T findViewById(@IdRes int id) {
        View view = getView();
        if (view == null) return null;
        return view.findViewById(id);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        if (outState != null) {
            Bundle arg = getArguments();
            if (arg != null && !arg.isEmpty())
                outState.putAll(arg);
            mSaver.save(outState);
        }
        super.onSaveInstanceState(outState);
    }

    public Bundle getExtra(Bundle savedInstanceState) {
        return savedInstanceState == null ? getArguments() : savedInstanceState;
    }

    public void setTitle(int title) {
        setTitle(getString(title));
    }

    public void addLeftAction(WindowUtil.TitleBarAttribute.Action action) {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).addLeftAction(action);
        }
    }

    public void addRightAction(WindowUtil.TitleBarAttribute.Action action) {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).addRightAction(action);
        }
    }

    public void removeLeftAction(WindowUtil.TitleBarAttribute.Action action) {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).removeLeftAction(action);
        }
    }

    public void removeRightAction(WindowUtil.TitleBarAttribute.Action action) {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).removeRightAction(action);
        }
    }

    public void notifyRightAction(WindowUtil.TitleBarAttribute.Action action) {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).notifyRightAction(action);
        }
    }

    public void removeAllLeftAction() {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).removeAllLeftActions();
        }
    }

    public void removeAllRightAction() {
        if (getContext() instanceof OriginActivity) {
            ((OriginActivity) getContext()).removeAllRightActions();
        }
    }

    protected void showToast(Throwable t) {
        if (t == null) return;
        String message = t.getMessage();
        if (TextUtils.equals(message, "Canceled") || TextUtils.equals(message, "Socket closed"))
            return;
        showToast(message);
    }

    protected void showToast(int t) {
        showToast(getString(t));
    }

    public void showToast(final String t) {
        final FragmentActivity activity = getActivity();
        if (activity == null) return;
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext().getApplicationContext(), t, Toast.LENGTH_SHORT).show();
            }
        });
    }

    protected void setResult(int resultCode, Intent data) {
        if (getActivity() != null)
            getActivity().setResult(resultCode, data);
    }

    protected void setResult(int resultCode) {
        if (getActivity() != null)
            getActivity().setResult(resultCode);
    }

    protected void finish() {
        if (getActivity() != null)
            getActivity().finish();
    }

    @Override
    public HashMap<String, Object> getFragments() {
        return mSaver.get();
    }

    @Override
    public void setFragments(HashMap<String, Object> fs) {
        mSaver.set(fs);
    }

    @Override
    public FragmentManager gFragmentManager() {
        return mSaver.fm();
    }

    public static class FragmentTitle {
        public int iconId;
        public int textId;
        public CharSequence text;

        public FragmentTitle(CharSequence text) {
            this.text = text;
        }

        public FragmentTitle() {
        }

        public FragmentTitle(int textId) {
            this.textId = textId;
        }

        public FragmentTitle(int iconId, int textId) {
            this.iconId = iconId;
            this.textId = textId;
        }

        public FragmentTitle(int iconId, int textId, CharSequence text) {
            this.iconId = iconId;
            this.textId = textId;
            this.text = text;
        }
    }
}
